import { _decorator, Component, Node } from 'cc';
import { Ins } from '../Ins';
import { ENUM_UI_LAYER } from '../Enum';
import { BaseLayer } from '../ui/BaseLayer';
import { PoolMgr } from './PoolMgr';
import { MainLayer } from '../ui/MainLayer';
const { ccclass, property } = _decorator;

@ccclass('UIMgr')
export class UIMgr extends Component {
    private layerMap = new Map<ENUM_UI_LAYER, BaseLayer>()

    onLoad() {
        Ins.initUIMgr(this)
    }

    init() {
        for (let type in ENUM_UI_LAYER) {
            const node: Node = PoolMgr.ins.getNode(ENUM_UI_LAYER[type], this.node.getChildByName('LAYERS'))
            if (node && !this.layerMap.has(ENUM_UI_LAYER[type])) {
                node.active = false
                node.addComponent(ENUM_UI_LAYER[type])
                this.layerMap.set(ENUM_UI_LAYER[type], node.getComponent(ENUM_UI_LAYER[type]))
            }
        }
    }

    isActive(type: ENUM_UI_LAYER) {
        if (this.layerMap.has(type)) {
            return this.layerMap.get(type).node.active
        }
        return false
    }

    getActiveLayers() {
        const layers: ENUM_UI_LAYER[] = []
        this.layerMap.forEach((layer: BaseLayer, type: ENUM_UI_LAYER) => {
            if (this.isActive(type)) layers.push(type)
        })
        return layers
    }

    toggle(type: ENUM_UI_LAYER, status: boolean = true, callback?: () => void) {
        if (this.layerMap.has(type)) {
            const base = this.layerMap.get(type)
            status ? base.show() : base.hide()
            callback && callback()
        }
    }

    resetMainLayer() {
        const layer: MainLayer = this.layerMap.get(ENUM_UI_LAYER.MAIN) as MainLayer
        layer?.reset()
    }

    setMainCountDown(bool: boolean = true) {
        const layer: MainLayer = this.layerMap.get(ENUM_UI_LAYER.MAIN) as MainLayer
        if (bool) {
            layer?.startCountDown()
        } else {
            layer?.stopCountDown()
        }
    }

    getMainSkillTip() {
        const layer: MainLayer = this.layerMap.get(ENUM_UI_LAYER.MAIN) as MainLayer
        return layer?.getSkillTip()
    }

    toggleMainSkillTip(bool: boolean) {
        const layer: MainLayer = this.layerMap.get(ENUM_UI_LAYER.MAIN) as MainLayer
        layer?.toggleSkillTip(bool)
    }
}


